<?php


namespace app\admin\model;


use think\facade\Request;
use think\Model;

class ShortLink extends Model
{
    protected $pk = 'link_id';

    protected $autoWriteTimestamp = true;

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        if (input('get.link_code')) $where[] = ['link_code', '=', input('get.link_code')];
        if (input('get.link_url')) $where[] = ['link_url', 'like', '%' . input('get.link_url') . '%'];
        $list = self::order('link_id', 'desc')
            ->where($where)
            ->paginate($limit);
        $domain = Request::domain();
        foreach ($list as $key => $item) {
            $list[$key]['expire_time'] = date('Y-m-d', $item['expire_time']);
            $list[$key]['link_code'] = $domain . '/' . $item['link_code'];
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function checkExist($linkId, $linkCode)
    {
        return self::where('link_id', '<>', $linkId)
            ->where('link_code', $linkCode)
            ->find();
    }

    public static function getLinkByCode($code)
    {
        return self::where('link_code', $code)
            ->where('expire_time', '>', time())
            ->find();
    }
}